// productdetail.js
var WxParse = require('../../utils/wxParse/wxParse.js');
var app = getApp();
var host = app.globalData.host
Page({
  data: {
    photoobject: [],
    image: app.globalData.image,
    productname: '',
    productprice: '',
    description: '',
    productcolor: '',
    productsize: '',
    openId: '',
    spuId: '',
    photolistAttr:'',
    ps:'',
    grounp:'',
    clothes:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    var that = this;
    wx.request({
      url: host + '/spuApi/spuProductDetail?id=' + id,
      header: { 'content-type': 'application/json' },
      success: function (res) {
        var photolistAttr = res.data.data.listAttr;
        var photo = res.data.data.photo;
        var photo1 = res.data.data.photo1;
        var photo2 = res.data.data.photo2;
        var photo3 = res.data.data.photo3;
        var photo4 = res.data.data.photo4;
        var photoobject = that.data.photoobject;
        if (photo) {
          var wxFilePath = wx.getStorageSync(that.data.image + photo)
          if (wxFilePath) {
            that.data.photoobject.push(wxFilePath);
          } else {
            that.data.photoobject.push(that.data.image + photo);
          }
          
        }
        if (photo1) {
          that.data.photoobject.push(that.data.image + photo1);
        }
        if (photo2) {
          that.data.photoobject.push(that.data.image + photo2);
        }
        if (photo3) {
          that.data.photoobject.push(that.data.image + photo3);
        }
        if (photo4) {
          that.data.photoobject.push(that.data.image + photo4);
        }

        var article = res.data.data.description.replace(/\/kaaction/g, that.data.image+"/kaaction")
        article = article.replace(/&amp;/g, "&");
        article = article.replace(/&quot;/g, "\"");
        article = article.replace(/&lt;/g, "<");
        article = article.replace(/&gt;/g, ">");
        WxParse.wxParse('article2', 'html', article, that, 5);
        that.setData({
          photolistAttr: photolistAttr,
          ps: photolistAttr[0].value,
          grounp: photolistAttr[1].value,
          clothes: photolistAttr[2].value,
          photoobject: photoobject,
          productname: res.data.data.name,
          productprice: res.data.data.price,
          description: article,
          productcolor: res.data.data.listColor,
          productsize: res.data.data.listSize,
          spuId: res.data.data.id
        })
      }
    })
  },
  //跳转至首页
  tohome: function () {
    wx.switchTab({
      url: '../index/index',
    })
  },
  //立即购买
  buynow: function (e) {
    wx.navigateTo({
      url: '../ordersure/photoordersure?id='+e.currentTarget.dataset.id
    })

  }
})